package arm.attestation.bean;

import arm.common.manager.TableManager;
import arm.attestation.entity.Metric;
import arm.attestation.entity.Obiekt;
import arm.db.tools.DBManager;
import arm.dic.entity.DicCategory;
import arm.dic.entity.DicMetricType;
import arm.dic.entity.DicPeriod;
import arm.dic.entity.DicPost;

import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.List;

public class MetricBean
{

    private Metric metric;
    private TableManager<Metric> tableManager;

    public Metric getMetric() {
        return metric;
    }

    public void setMetric(Metric metric) {
        this.metric = metric;
    }

    public String viewTable ()
    {
        getTableManager().readFromDB();
        return "viewMetricTable" ;
    }

    public TableManager<Metric> getTableManager () {
        if (tableManager==null)
        {
            tableManager=new TableManager<Metric>(Metric.class);
        }
        return tableManager;
    }

    public void setTableManager (TableManager<Metric> tableManager) {
        this.tableManager=tableManager;
    }

    public String create()
    {
        metric = new Metric();
        return "editMetric";
    }

    public String save()
    {
        DBManager.saveOrUpdate(getMetric());
        return viewTable();
    }

    public String edit () {
        setMetric(getTableManager().getSelectedObj());
        return "editMetric";
    }

    public  String cancel () {
        return viewTable();
    }

    public void delete () {
        DBManager.delete(getTableManager().getSelectedObj());
        getTableManager().readFromDB();
    }

    public List<SelectItem> getMetricList()
    {
        List<DicMetricType> objs = DBManager.readList(DicMetricType.class.getName());
        List<SelectItem> selectItems = new ArrayList();
        for (DicMetricType obj : objs)
        {
            selectItems.add(new SelectItem(obj, obj.getName()));
        }
        return selectItems;

    }

     public List<SelectItem> getPostList()
    {
        List<DicPost> objs = DBManager.readList(DicPost.class.getName());
        List<SelectItem> selectItems = new ArrayList();
        for (DicPost obj : objs)
        {
            selectItems.add(new SelectItem(obj, obj.getName()));
        }
        return selectItems;

    }

     public List<SelectItem> getPeriodList()
    {
        List<DicPeriod> objs = DBManager.readList(DicPeriod.class.getName());
        List<SelectItem> selectItems = new ArrayList();
        for (DicPeriod obj : objs)
        {
            selectItems.add(new SelectItem(obj, obj.getName()));
        }
        return selectItems;

    }

    public List<SelectItem> getObiektList()
    {
        List<Obiekt> objs = DBManager.readList(Obiekt.class.getName());
        List<SelectItem> selectItems = new ArrayList();
        for (Obiekt obj : objs)
        {
            selectItems.add(new SelectItem(obj, obj.getName()));
        }
        if (objs != null && objs.size() > 0 && metric != null && metric.getObiekt() == null)
        {
            metric.setObiekt(objs.get(0));
        }
        return selectItems;

    }

    public List<SelectItem> getCategoryList()
    {
        List<SelectItem> selectItems = new ArrayList();
        if (getMetric() != null && getMetric().getDicMetricType() != null){
            List<DicCategory> objs = DBManager.readList(DicCategory.class.getName()
                    + " as t WHERE t.dicMetricType.id="
                    + getMetric().getDicMetricType().getId());
            for (DicCategory obj : objs)
            {
                selectItems.add(new SelectItem(obj, obj.getName()));
            }
        }
        return selectItems;
    }

}
